<template>

  <div>

    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>救助站管理</el-breadcrumb-item>
      <el-breadcrumb-item>救助站列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <div>
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item label="名称 : ">
              <el-input v-model="name" placeholder="请输入救助站名称" clearable @clear="searchRS"></el-input>
            </el-form-item>

            <el-form-item label="地址 : ">
              <el-input v-model="address" placeholder="请输入救助站地址" clearable @clear="searchRS"></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click.prevent="searchRS">查询</el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible=true">添加救助站</el-button>
        </el-col>

      </el-row>
      <el-table :data="RSList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="电话📞" prop="telephone"></el-table-column>
        <el-table-column label="地址" prop="address"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="简介" prop="introduce"></el-table-column>
        <el-table-column label="创建时间" prop="insertTime"></el-table-column>
        <el-table-column label="更新时间" prop="updateTime"></el-table-column>


        <el-table-column label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.del == '0'" style="color: #67C23A">正常</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" :width="'145px'">
          <template slot-scope="scope">
            <el-button type="warning" size="mini" @click="showEditDialog(scope.row.id)">编辑</el-button>
            <el-button type="danger" size="mini" @click="removeRSById(scope.row.id)">注销</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[3, 6 , 12, 24]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </el-card>


    <!--添加救助站-->
    <el-dialog title="添加救助站" :visible.sync="addDialogVisible" width="50%" :close-on-click-modal="false"
               @close="addRSFormClose">
      <el-form
          :model="addRSForm"
          :rules="addRSFormRules"
          ref="addRSFormRef"
          label-width="100px"
          class="demo-ruleForm"
      >
        <el-form-item label="救助站名称" prop="name">
          <el-input v-model="addRSForm.name"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="telephone">
          <el-input v-model="addRSForm.telephone"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="addRSForm.address"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addRSForm.email"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="introduce">
          <el-input v-model="addRSForm.introduce"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addRS('addRSFormRef')">确 定</el-button>
        <el-button @click="cancelAddRS">取 消</el-button>
      </span>
    </el-dialog>


    <!--修改用戶-->
    <el-dialog
        title="修改救助站"
        :visible.sync="editDialogVisible"
        width="50%"
        :close-on-click-modal="false"
        @close="editRSFormClose"
    >
      <el-form
          :model="editRSForm"
          :rules="editRSFormRules"
          ref="editRSFormRef"
          label-width="100px"
          class="demo-ruleForm"
      >
        <el-form-item label="救助站名称" prop="name">
          <el-input v-model="editRSForm.name"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="telephone">
          <el-input v-model="editRSForm.telephone"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="editRSForm.address"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editRSForm.email"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="introduce">
          <el-input v-model="editRSForm.introduce"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editRS('editRSFormRef')">确 定</el-button>
        <el-button @click="resetEditForm">取 消</el-button>
      </span>
    </el-dialog>


  </div>

</template>

<script>

export default {
  data() {
    return {
      editDialogVisible: false,
      swflag: false,
      pageNum: 1,
      pageSize: 3,
      RSList: [],
      total: 0,
      name: "",
      address: "",
      addDialogVisible: false,
      addRSForm: {
        name: "",
        telephone: "",
        address: "",
        email: "",
        introduce: ""
      },
      editRSForm: {
        id: "",
        name: "",
        telephone: "",
        address: "",
        email: "",
        introduce: ""
      },
      editRSFormRules: {
        name: [
          {required: true, message: "請輸入名称", trigger: "blur"},
          {min: 1, max: 20, message: "名称必须是1-20个字符", trigger: "blur"}
        ],
        telephone: [
          {required: true, message: "請輸入联系方式", trigger: "blur"},
          // 数字验证问题还未解决
          {min: 11, max: 11, message: "名称必须是11符", trigger: "blur"}
        ],
        address: [
          {required: true, message: "請輸入地址", trigger: "blur"},
          {min: 1, max: 40, message: "地址不能为空", trigger: "blur"}
        ],
        email: [
          {required: true, message: '邮箱不能为空', trigger: 'blur'},
          {
            pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            message: '必须符合邮箱格式',
            trigger: 'blur'
          }
        ],
        del: [{required: true, message: "请选择救助站状态", trigger: "blur"}]
      },
      addRSFormRules: {
        name: [
          {required: true, message: "請輸入名称", trigger: "blur"},
          {min: 1, max: 20, message: "名称必须是1-20个字符", trigger: "blur"}
        ],
        telephone: [
          {required: true, message: "請輸入联系方式", trigger: "blur"},
          // 数字验证问题还未解决
          {min: 11, max: 11, message: "名称必须是11符", trigger: "blur"}
        ],
        address: [
          {required: true, message: "請輸入地址", trigger: "blur"},
          {min: 1, max: 40, message: "地址不能为空", trigger: "blur"}
        ],
        email: [
          {required: true, message: '邮箱不能为空', trigger: 'blur'},
          {
            pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            message: '必须符合邮箱格式',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  methods: {
    async removeRSById(id) {
      // console.log(id + "****************");
      this.$confirm('此操作将移动该数据到历史列表, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = "/rescueStation/del/" + id;
        this.$axios.delete(url).then(res => {
          if (res.data.code == 200) {
            this.$message({
              type: 'success',
              message: '注销成功'
            });
            this.initRS();
          } else {
            this.$message({
              type: 'error',
              message: res.data.msg,
              duration: 3000
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    showEditDialog(id) {
      let url = "/rescueStation/getRSByKey/" + id;
      this.$axios.get(url).then(res => {
        if (res.data.code == 200) {
          this.editRSForm = res.data.data;
          this.editDialogVisible = true;
        } else {
          this.$message({
            type: 'error',
            message: res.data.msg,
            duration: 1000
          })
        }
      })
    },
    editRSFormClose() {
      this.$refs.editRSFormRef.resetFields();
    },
    editRS(bbb) {
      this.$refs[bbb].validate((valid) => {  //开启校验
        if (valid) {   // 如果校验通过，请求接口，允许提交表单
          let url = "/rescueStation/modify";
          console.log(this.editRSForm)
          this.$axios.put(url, this.editRSForm)
              .then(res => {
                if (res.data.code == 200) {
                  this.$message({
                    type: "success",
                    message: res.data.msg,
                    duration: 1000
                  })
                  this.editDialogVisible = false;
                  this.initRS();
                  // this.initRole();
                } else {
                  this.$message({
                    type: 'error',
                    message: res.data.msg,
                    duration: 1000
                  })
                }
              })
        } else {   //校验不通过
          return false;
        }
      });
    },
    resetEditForm() {
      this.$refs.editRSFormRef.resetFields();//重置恢复之前的数据
      this.initRS();
      this.editDialogVisible = false;
    },
    addRS(aaa) {
      this.$refs[aaa].validate((valid) => {  //开启校验
        if (valid) {   // 如果校验通过，请求接口，允许提交表单
          let url = "/rescueStation/addRS";
          console.log(this.addRSForm)
          this.$axios.post(url, this.addRSForm)
              .then(res => {
                if (res.data.code == 200) {
                  this.initRS();
                  this.$message({
                    type: 'success',
                    message: '添加成功'
                  });
                  this.addDialogVisible = false;
                } else {
                  this.$message({
                    type: 'error',
                    message: res.data.msg,
                    duration: 1000
                  })
                }
              })
        } else {   //校验不通过
          return false;
        }
      });

    },

    //关闭管理员的对话框
    addRSFormClose() {
      this.addDialogVisible = false;
      this.$refs.addRSFormRef.resetFields();
    },
    //取消添加管理员
    cancelAddRS() {
      this.addDialogVisible = false;
      this.$refs.addRSFormRef.resetFields();
    },


    searchRS() {
      this.pageNum = 1;
      this.initRS();
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.pageNum = 1;
      this.initRS();
    },
    handleCurrentChange(newPage) {
      this.pageNum = newPage;
      this.initRS();
    },
    //初始化查询 用户信息
    initRS() {
      let url = "/rescueStation/listRS";
      this.$axios.get(url, {
        params: {
          now: this.pageNum,
          size: this.pageSize,
          name: this.name,
          address: this.address
        }
      }).then(res => {
        if (res.data.code == 200) {
          this.RSList = res.data.data.list;
          this.pageSize = res.data.data.pageSize;
          this.pageNum = res.data.data.pageNum;
          this.total = res.data.data.total;
        } else {
          this.$message({
            type: 'error',
            message: res.data.msg,
            duration: 1000
          })
        }
      })
    }

  },
  created() {
    this.initRS();
  }
};


</script>

<style scoped>

</style>